package com.example.demo.controller;

import com.example.demo.entity.BaseResponse;
import com.example.demo.entity.PersonBean;
import com.example.demo.entity.PersonSearchBean;
import com.example.demo.entity.ResultBody;
import com.example.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PersonController {

    @Autowired
    PersonService personService;

    @RequestMapping("/personListTask")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<PersonBean> personListTask() {
        List<PersonBean> taskList = personService.personFindAll();
        return new BaseResponse<PersonBean>(0, "success",
                taskList.size(), taskList);
    }

    @RequestMapping("/personSaveTask")
    @ResponseBody
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResultBody personSaveTask(PersonBean personBean){
        System.out.println(personBean.toString());
        if(personService.personSaveTask(personBean))
            return new ResultBody(0, "success");
        else
            return new ResultBody(1, "fail");
    }

    @RequestMapping("/personDelTask")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResultBody personDelTask(int id){
        if(personService.personDelTask(id))
            return new ResultBody(0, "success");
        else
            return new ResultBody(1, "fail");
    }

    @RequestMapping("/personSearchTask")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<PersonBean> personSearchTask(PersonSearchBean personSearchBean){
        personSearchBean.setStart((personSearchBean.getPage()-1)*personSearchBean.getLimit());
        List<PersonBean> taskList = personService.personSearchTask(personSearchBean);
        return new BaseResponse<PersonBean>(0, "success",
                personService.personSearchCount(personSearchBean), taskList);
    }
}
